package com.ruoyi.chat.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.jwyz.dto.TargetUserResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.chat.domain.Blacklist;
import com.ruoyi.chat.service.IBlacklistService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 黑名单Controller
 *
 * @author lxp
 * @date 2023-03-31
 */

@RestController
@Api(tags = "黑名单模块")
@RequestMapping("/chat/blacklist")
public class BlacklistController extends BaseController {
    @Autowired
    private IBlacklistService blacklistService;



    /**
     * 查询黑名单列表
     */
    @ApiOperation("查询黑名单列表")
    @ApiImplicitParam(name = "blacklist", value = "黑名单对象", dataType = "Blacklist")
    @GetMapping("/list")
    public TableDataInfo list(Blacklist blacklist) {
        startPage();
        List<Blacklist> list = blacklistService.selectBlacklistList(blacklist);
        System.out.println(SecurityUtils.getUserId());
        return getDataTable(list);
    }

    /**
     * 导出黑名单列表
     */
    @ApiOperation("导出黑名单列表")
    @ApiImplicitParam(name = "blacklist", value = "黑名单对象", dataType = "Blacklist")
//    @PreAuthorize("@ss.hasPermi('chat:blacklist:export')")
    @Log(title = "黑名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Blacklist blacklist) {
        List<Blacklist> list = blacklistService.selectBlacklistList(blacklist);
        ExcelUtil<Blacklist> util = new ExcelUtil<Blacklist>(Blacklist.class);
        util.exportExcel(response, list, "黑名单数据");
    }

    /**
     * 获取黑名单详细信息
     */
    @ApiOperation("获取黑名单详细信息")
    @ApiImplicitParam(name = "id", value = "黑名单ID", required = true, dataType = "Long", paramType = "path")
//    @PreAuthorize("@ss.hasPermi('chat:blacklist:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(blacklistService.selectBlacklistById(id));
    }

    /**
     * 新增黑名单
     */
    @ApiOperation("新增黑名单")
    @ApiImplicitParam(name = "blacklist", value = "黑名单对象", dataType = "Blacklist")
//    @PreAuthorize("@ss.hasPermi('chat:blacklist:add')")
    @Log(title = "黑名单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Blacklist blacklist) {
        return toAjax(blacklistService.insertBlacklist(blacklist));
    }

    /**
     * 修改黑名单
     */
    @ApiOperation("修改黑名单")
    @ApiImplicitParam(name = "blacklist", value = "黑名单对象", dataType = "Blacklist")
//    @PreAuthorize("@ss.hasPermi('chat:blacklist:edit')")
    @Log(title = "黑名单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Blacklist blacklist) {
        return toAjax(blacklistService.updateBlacklist(blacklist));
    }

    /**
     * 删除黑名单
     */
    @ApiOperation("删除黑名单")
    @ApiImplicitParam(name = "ids", value = "黑名单ID数组", required = true, dataType = "Long", paramType = "path")
//    @PreAuthorize("@ss.hasPermi('chat:blacklist:remove')")
    @Log(title = "黑名单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(blacklistService.deleteBlacklistByIds(ids));
    }

    /**
     * 用户个人黑名单的查询
     */
    @ApiOperation("用户个人黑名单的查询")
    @ApiImplicitParam(name = "uid", value = "用户ID", required = true, dataType = "long", paramType = "path")
    @Anonymous
    @GetMapping("getBlockedUsers/{uid}")
    public TableDataInfo getBlacklist(@PathVariable("uid") long uid) {
        List list = blacklistService.selectTargetUserByUserId(uid);
        return getDataTable(list);
    }

    /**
     * 用户移除黑名单
     */
    @ApiOperation("用户移除黑名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "targetUserId", value = "目标用户ID", required = true, dataType = "long", paramType = "path")
    })
    @Anonymous
    @DeleteMapping("DeleteBlacklist/{uid}/{targetUserId}")
    public AjaxResult DeleteBlacklist(@PathVariable("uid") long uid, @PathVariable("targetUserId") long targetUserId) {

        return success(blacklistService.deleteBlacklistArgentUserId(uid, targetUserId));
    }

    /**
     *  获取登录用户的黑名单
     * @return
     */
    @ApiOperation("获取登录用户的黑名单")
    @Anonymous
    @GetMapping("/getUserInfoByTargetUserId")
    public AjaxResult getUserInfoByTargetUserId() {
        // 根据登录用户的uid筛选target_user_id
        Long userId = SecurityUtils.getUserId();
/*        List<TargetUserResult> targetUserIds = blacklistService.selectTargetUserByUserId(userId);
        List<Long> userIdList = new ArrayList<>();
        for (TargetUserResult results : targetUserIds) {
            userIdList.add(results.getTargetUserId());
        }*/
        // 根据target_user_id查询出用户信息
        List<TargetUserResult> userInfos = blacklistService.selectUserInfoListById(userId);
        return AjaxResult.success(userInfos);
    }

}
